package com.zhijian.medical.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import com.zhijian.medical.annotation.CurrentUser;
import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.config.CommonConfig;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.po.Device;
import com.zhijian.medical.entity.po.MedicalDevice;
import com.zhijian.medical.entity.vo.request.DeviceSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.DeviceSearchVo;
import com.zhijian.medical.entity.vo.response.CodeVo;
import com.zhijian.medical.entity.vo.response.DeviceVo;
import com.zhijian.medical.enums.DeviceTypeEnum;
import com.zhijian.medical.service.DeviceService;
import com.zhijian.medical.service.impl.DeviceServiceImpl;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 医疗设备 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-01-01 15:37:21
 */
@Slf4j
@Controller
@Api(tags = "医疗设备")
public class DeviceController {

    @Autowired
    private DeviceService service;

    @Autowired
    private CommonConfig commonConfig;

    /**
     * 获取最新编号们
     *
     * @return
     */
    @Response
    @GetMapping("/device/code")
    @ApiOperation("获取最新编号们")
    public CodeVo getNewCode() {
        CodeVo codeVo = new CodeVo();
        codeVo.setCode2(service.createNextCode());
        codeVo.setCode(DeviceServiceImpl.CODE_PREFIX);
        codeVo.setDw(commonConfig.getDw());
        codeVo.setVersion(commonConfig.getVersion());
        codeVo.setSaveTime(commonConfig.getSaveTime());
        codeVo.setZrr(commonConfig.getZrr());
        codeVo.setPublishDate(DateUtil.format(new Date(), FastDateFormat.getInstance("yyyy年MM月dd日")));
        codeVo.setExecuteDate(codeVo.getPublishDate());
        return codeVo;
    }

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("device")
    @ApiOperation("新增")
    public boolean insert(@RequestBody DeviceSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.save(param);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("device/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("device/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody DeviceSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.modify(param);
    }

    /**
     * 修改状态
     *
     * @param id
     * @param status
     * @return
     */
    @Response
    @PutMapping("device/status/{id}")
    @ApiOperation("修改状态")
    public boolean updateStatus(@PathVariable("id") Long id,
                                @ApiParam(name = "status", value = "状态：检修/运行/封存/待机") @RequestParam(value = "status") String status) {
        return service.updateStatus(id, status);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("device/{id}")
    @ApiOperation("根据id查询")
    public DeviceVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectVoById(id);
    }

    /**
     * 查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("device/page")
    @ApiOperation("列表查询")
    public Page<DeviceVo> selectByCondition(@CurrentUser @ApiIgnore PersonDto personDto, @RequestBody(required = false) DeviceSearchVo param) {
        if (param == null) param = DeviceSearchVo.builder().build();
        //todo 暂时注释掉
//        param.setGroupIds(personDto.getGroupIds());
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 不分页的列表查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("device/list/with/maintenance")
    @ApiOperation("不分页的列表查询，目前只给维护用")
    public List<DeviceVo> selectList(@CurrentUser @ApiIgnore PersonDto personDto, @RequestBody(required = false) DeviceSearchVo param) {
        if (param == null) param = DeviceSearchVo.builder().build();
        //todo 暂时注释掉
//        param.setGroupIds(personDto.getGroupIds());
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectListWithMaintenance(param);
    }

    /**
     * 查询扩增仪列表
     *
     * @return
     */
    @Response
    @GetMapping("device/kzy/list")
    @ApiOperation("查询扩增仪列表")
    public List<Device> selectKzyList(@RequestParam(required = false) String type) {
        if (type == null) {
            type = DeviceTypeEnum.KZY.getRealName();
        }
        return service.selectListByType(type);
    }

    /**
     * 查询核酸提取仪列表
     *
     * @return
     */
    @Response
    @GetMapping("device/hstqy/list")
    @ApiOperation("查询核酸提取仪列表")
    public List<Device> selectHstqyList() {
        return service.selectListByType(DeviceTypeEnum.HSTQY.getRealName());
    }

    /**
     * 查询rfid标签位置
     * x
     *
     * @return
     */
    @Response
    @GetMapping("device/rfid/list")
    @ApiOperation("查询rfid标签位置")
    public List<MedicalDevice> selectRfidList() {
        return service.selectRfidList();
    }

    /**
     * 获取所有的设备类型列表
     *
     * @return
     */
    @Response
    @GetMapping("device/type/list")
    @ApiOperation("获取所有的设备类型列表")
    public List<String> createDeviceTypeList() {
        DeviceTypeEnum[] deviceTypeEnums = DeviceTypeEnum.values();
        return Arrays.stream(deviceTypeEnums).map(DeviceTypeEnum::getRealName).collect(Collectors.toList());
    }

}